/*
 * Copyright (C) 2013 Sony Computer Entertainment Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef HTMLTile_h
#define HTMLTile_h

#if ENABLE(MANX_HTMLTILE)

#include "ActiveDOMObject.h"
#include "EventTarget.h"
#include "ImageData.h"
#include "StringCallback.h"

#include <wtf/RefCounted.h>
#include <wtf/PassRefPtr.h>
#include <wtf/text/WTFString.h>
#include <wtf/HashMap.h>

namespace WebCore {

class ScriptExecutionContext;
class HTMLTileClient;

    class HTMLTile : public RefCounted<HTMLTile>, public EventTarget, public ActiveDOMObject {
    public:
        static PassRefPtr<HTMLTile> create(ScriptExecutionContext*, int width, int height);
        virtual ~HTMLTile();

        // Must be kept in sync with the values defined in HTMLTile.idl!
        static const int FocusIn = 0;
        static const int FocusOut = 1;
        static const int ButtonPress = 2;
        static const int ButtonRelease = 3;

        void destroy();

        bool isValid() const;
        bool isPaused() const;

        int width() const;
        int height() const;
        PassRefPtr<ImageData> imageData() const;

        String url() const;
        void setUrl(const String& url);

        void pause();
        virtual void resume(); // overrides ActiveDOMObject::resume()
        void sendEvent(unsigned short eventType);
        void eval(const String& jsScript, PassRefPtr<StringCallback>);

        DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
        DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
        DEFINE_ATTRIBUTE_EVENT_LISTENER(update);
        DEFINE_ATTRIBUTE_EVENT_LISTENER(locationchange);
        DEFINE_ATTRIBUTE_EVENT_LISTENER(unresponsive);
        DEFINE_ATTRIBUTE_EVENT_LISTENER(crash);

        void dispatchOnLoadEvent();
        void dispatchOnUpdateEvent();
        void dispatchOnErrorEvent(int errorCode);
        void dispatchOnLocationChangeEvent(const String& url);
        void dispatchOnUnresponsiveEvent();
        void dispatchOnCrashEvent();
        void dispatchEvalCallback(uint64_t callbackID, const String& result);

        using RefCounted<HTMLTile>::ref;
        using RefCounted<HTMLTile>::deref;

        // EventTarget interface
        virtual const AtomicString& interfaceName() const;
        virtual ScriptExecutionContext* scriptExecutionContext() const;

        // ActiveDOMObject interface
        virtual bool canSuspend() const;
        virtual void suspend(ReasonForSuspension);
        virtual void stop();

        void setValid();

    private:
        HTMLTile(ScriptExecutionContext*, int width, int height);

        void attachToBackend();
        void detachFromBackend();

        // EventTarget interface
        virtual void refEventTarget() { ref(); }
        virtual void derefEventTarget() { deref(); }
        virtual EventTargetData* eventTargetData();
        virtual EventTargetData* ensureEventTargetData();

        enum TileState {
            Invalid = 0x00,
            Valid = 0x01,
            Paused = 0x10,
        };
        typedef int TileStates; // TileState combination
        void setState(TileState);
        void resetState(TileState);

        int m_width;
        int m_height;
        RefPtr<ImageData> m_data;
        String m_url;
        TileStates m_state;

        HTMLTileClient* m_client;
        uint64_t m_tileID;

        EventTargetData m_eventTargetData;

        uint64_t m_nextCallbackID;
        HashMap<uint64_t, RefPtr<StringCallback> > m_evalCallbacks;
    } ;

}  // namespace WebCore

#endif // ENABLE(MANX_HTMLTILE)

#endif // HTMLTile_h
